package com.fh.controller.system.web3jTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import javax.mail.Header;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import com.googlecode.jsonrpc4j.Base64;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.mysql.jdbc.log.LogUtils;

import net.sf.json.JSONObject;

public class BittcoinTests {
	public static void main(String[] args) throws Throwable {
		// TODO Auto-generated method stub
//        String a= doPost("http://192.168.1.14:8333/'{'jsonrpc': '1.0', 'id':'curltest', 'method': 'getnewaddress', 'params': '[]' }'");
//		JSONObject json = new JSONObject();
//		json.put("jsonrpc", "1.0");
//		json.put("id", "curltest");
//		json.put("method", "getnewaddress");
//		json.put("params", "[]");
		BittcoinTests s = new BittcoinTests();
		try{
			String cred = Base64.encodeBytes(("sss:admin123").getBytes());
			Map<String, String> headers = new HashMap<String, String>(1);
			//身份认证
			headers.put("Authorization", "Basic " + cred);
			
			JsonRpcHttpClient client = new JsonRpcHttpClient(
		             new URL("http://192.168.1.14:8036"),headers);
//	       String result =s.dumpprivkey(client,"3AYeXyy2dxPKUm3DEHzhrAmaz2dG9LuiBY");
			String result =s.getInfo(client);
//			String results =s.validateaddress(result,client);
//	   	String results =s.getNewaddress(client,"3AYeXyy2dxPKUm3DEHzhrAmaz2dG9LuiBY","a9146123f3221f58e480586c0c115ed17302a6c76ecb87");
			
			
	       System.out.println(result);
		}catch(Exception e){ 
			e.printStackTrace();
		}
		
	}


   
	/**
	 * 转账到制定的账户中
	 * @param address
	 * @param amount
	 * @return
	 * @throws Throwable
	 */
	public String sendtoaddress(String address,double amount,JsonRpcHttpClient client)throws Throwable{
		return  (String) client.invoke("sendtoaddress", new Object[] {address,amount}, Object.class).toString();
	}

	
	/**
	 * 生成新的接收地址
	 * @return
	 * @throws Throwable
	 */
	public String getNewaddress(JsonRpcHttpClient client) throws Throwable{
		return  (String) client.invoke("getnewaddress", new Object[] {}, Object.class).toString();
	}
	
	
	/**
	 * 显示与对应的私钥。
	 * @return
	 * @throws Throwable
	 */
	public String dumpprivkey(JsonRpcHttpClient client,String account) throws Throwable{
		return  (String) client.invoke("dumpprivkey", new Object[] {account}, Object.class).toString();
	}
	
	/**
	 * 获取钱包信息
	 * @return
	 * @throws Throwable
	 */
	public String getInfo(JsonRpcHttpClient client) throws Throwable{
		return (String) client.invoke("getinfo", new Object[] {}, Object.class).toString();
	}
   
	/**
	 * 验证地址是否存在
	 * @param address
	 * @return
	 * @throws Throwable
	 */
	public String validateaddress(String address,JsonRpcHttpClient client)throws Throwable{
		return  (String) client.invoke("validateaddress", new Object[] {address}, Object.class).toString();
	}

	
	
	

}
